﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp._console.core
{
    public partial class theme_edit : System.Web.UI.Page
    {
        private Guid _themeId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                _themeId = new Guid(Request.QueryString["id"]);
            }

            if (!IsPostBack)
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();

                if (_themeId != Guid.Empty)
                {
                    SmartSpace.Core.SiteTemplate template = service.GetSiteTemplate(_themeId);
                    if (template != null)
                    {
                        _ctlEditCode.Text = template.Code;
                        _ctlEditName.Text = template.Name;
                    }
                }

                _ctlLinkBack.NavigateUrl = "./theme-list.aspx";
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();

            SiteTemplate template = service.GetSiteTemplate(_themeId);

            template.Name = _ctlEditName.Text;

            if (service.SaveSiteTemplate(template))
            {
                Response.Redirect("./theme-list.aspx");
            }
        }
    }
}